﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AssessmentActions;

public partial class Teacher_ViewTestResults : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnFetchFormattedForTesting_Click(object sender, EventArgs e)
    {
        string resultString = "";

        string userId = ddlStudents.SelectedItem.Value.ToString();
        string testName = ddlTestNames.SelectedItem.Value.ToString();

        AssessmentDatabaseActions ada = new AssessmentDatabaseActions(System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString);

        string correctAnswers = ada.getCorrectAnswers(testName);
        string[] correctAnswerArray = correctAnswers.Split('|');

        string[] answerString = ada.fetchTestResultsForUser(userId, testName);

        

        if (answerString.Length == 0)
        {
            resultString = "This user has not taken this assessment.";
        }

        int i = 0;
        foreach (string answers in answerString)
        {
            string grade = ada.calculateAssessmentGrade(testName, answers);


            resultString += "<p>Attempt " + i + "<br />";
            resultString += "Grade: " + grade + "%<br />";

            string[] words = answers.Split('|');
            int j = 0;
            foreach (string word in words)
            {
                resultString += "Question: " + j + " - ";
                resultString += "Correct Answer: " + correctAnswerArray[j] + " - ";
                resultString += "Selected Answer: " + word + "<br />";
                
                j++;
            }
            resultString += "</p>";
            i++;
        }



        lblResults.Text = resultString + "<br />";
    }
}